package main

import (
	"mytest/week9/pkg/bufio"
	"mytest/week9/pkg/bytes"
	"mytest/week9/protocol"
	"net"
)

var seq int32 = 1

func main() {
	conn, _ := net.Dial("tcp", "127.0.0.1:10000")
	defer conn.Close()

	// 读写缓冲区
	wr := bufio.NewWriter(conn)
	rr := bufio.NewReader(conn)

	handle(wr, rr)

}

func handle(wr *bufio.Writer, rr *bufio.Reader) {
	sendMessage(wr, "hello")
}

func sendMessage(wr *bufio.Writer, message string) {
	pro := &protocol.Proto{
		Op:   112,
		Ver:  1,
		Seq:  seq,
		Body: []byte(message),
	}
	seq += 1
	bwr := bytes.NewWriterSize(len(message) + 64)
	_ = pro.WriteTo(bwr)
	_, _ = wr.Write(bwr.Buffer())
	wr.Flush()
}
